/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clases;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;


/**
 *
 * @author Ele
 */
public class abmBovino {
    

public void Conectar(){
        try{
            mdbc = new conexion();
            mdbc.init();
            Connection conn= mdbc.getMyConnection();
            stmt = conn.createStatement();
        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Problemas al concetarse a la Base de Datos","Error",JOptionPane.ERROR_MESSAGE);
        }
    }   


public boolean ExisteBovino(String idTag){
        int b= 0;
        try{
            Conectar();
            ResultSet rs = stmt.executeQuery("select idsenasa from bovino where idSenasa  = '"+idTag+"'");
            System.out.println(rs.first());
            if(rs.first()){

            b=1;

            }
        }catch(Exception ex){

                ex.printStackTrace();
        }
        if(b==1)return true;
        else return false;
    }




//ALTA BOVINO

public void AltaBovino(String idSenasa, String idTag)   {
//      String ClaveCod= md5(Clave);
      
    
    
    String comandoAlta= "Insert into bovino values ('"+idSenasa+"','"+idTag+"')";
      
      try{
          Conectar();
          
            int done= stmt.executeUpdate(comandoAlta);
            //JOptionPane.showMessageDialog(null,"alta","Aviso",JOptionPane.INFORMATION_MESSAGE);
            mdbc.close(stmt);
          
        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null,ex.getMessage(),"Error en alta bovino",JOptionPane.ERROR_MESSAGE);
        }
      mdbc.destroy();
      }

public void AltaLectura(String fecha, String hora)   {
//      String ClaveCod= md5(Clave);
      String comandoAlta= "Insert into lectura (fecha,hora) values ('"+fecha+"','"+hora+"')";
      
      try{
          Conectar();
          int done= stmt.executeUpdate(comandoAlta);
            //JOptionPane.showMessageDialog(null,"alta","Aviso",JOptionPane.INFORMATION_MESSAGE);
            
        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null,ex.getMessage(),"Error en alta bovino",JOptionPane.ERROR_MESSAGE);
        }
      mdbc.close(stmt);
      }
public int getIdLectura() {
//      String ClaveCod= md5(Clave);
      int IdLectura = 0;
      String cmd= "select max(idlectura) from lectura";
      
      try{
          Conectar();
          ResultSet rs = stmt.executeQuery(cmd);
            //JOptionPane.showMessageDialog(null,"alta","Aviso",JOptionPane.INFORMATION_MESSAGE);
          rs.first();
          IdLectura =Integer.valueOf(rs.getString("max(idlectura)"));
        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null,ex.getMessage(),"Error en getIdLectura",JOptionPane.ERROR_MESSAGE);
        }
     
      mdbc.close(stmt);
      return IdLectura;
      }


public void AltaDetalleLectura(Float Peso, String idTag)   {
//      String ClaveCod= md5(Clave);
      String cmd= "Insert into detalle_lectura values ('"+Peso+"','"+getIdLectura()+"','"+idTag+"')";
      
      try{
          Conectar();
          int done= stmt.executeUpdate(cmd);
            //JOptionPane.showMessageDialog(null,"alta","Aviso",JOptionPane.INFORMATION_MESSAGE);
            
        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null,ex.getMessage(),"Error en AltaDetalleLectura",JOptionPane.ERROR_MESSAGE);
        }
      mdbc.close(stmt);
      }


public void BajaBovino(String idTag){
      String comandoBaja= "Delete  from bovino where idTag='"+idTag+"'";
      try{
          Conectar();
              int done = stmt.executeUpdate(comandoBaja);
              
              
        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null,"Error al dar de baja bovino","ERROR",JOptionPane.ERROR_MESSAGE);
        }
      
  }   

  private conexion mdbc;
  private Statement stmt;


}